02 / 04

How does context.Context propagate cancellation across goroutines and calls?

Context forms a tree where cancellation propagates from parent to all children. Every function that may block or do I/O should accept a ctx as its first parameter and respect its cancellation.

Context propagation
Context best practices
  1. 1

    Pass context as the first parameter — never store it in a struct

  2. 2

    Always call the cancel function returned by WithCancel/WithTimeout — even if the operation completes

  3. 3

    Use context.WithValue only for request-scoped metadata (trace IDs, auth tokens) — not for optional parameters

  4. 4

    Respect ctx.Done() in goroutines: select on it alongside work channels

  5. 5

    Check ctx.Err() to distinguish context.Canceled from context.DeadlineExceeded

Difficulty: 5/10
Topics: cancellation propagation, deadline handling, context hierarchy

Scenario Questions

0-2 years experience
  1. 1

    You have a worker goroutine reading from a channel. How would you use a Context so the worker stops when the caller cancels?

  2. 2

    If you pass a parent context with a timeout into a function that spawns its own goroutine, what happens to that goroutine when the timeout expires?

  3. 3

    What will happen if a long‑running loop ignores the context’s Done channel?

2-5 years experience
  1. 1

    After deploying an HTTP handler that launches several downstream goroutines with their own WithCancel contexts, we see goroutines hanging after the client disconnects. Walk me through how you’d debug the cancellation propagation.

  2. 2

    In a batch job we wrap the top‑level context with a deadline, but inner functions create child contexts with WithCancel and never defer cancel. Explain the impact on resources and how you’d fix it.

  3. 3

    Why might a deeper call receive a nil context value even though the parent passed a non‑nil context, and how does that affect cancellation?

5-8 years experience
  1. 1

    Design a request‑handling pipeline where each stage runs in its own goroutine and must respect client cancellation. How do you structure the contexts to avoid leaks and ensure timely shutdown?

  2. 2

    A service forwards a request to multiple downstream services in parallel, each with its own timeout. How would you combine their contexts to propagate a single cancellation signal while still handling individual deadlines?

  3. 3

    What are the performance implications of creating many short‑lived contexts in a tight loop, and how would you mitigate any overhead in a latency‑critical path?

8+ years experience
  1. 1

    We’re migrating legacy code that uses custom cancellation channels to the standard context package. What strategy would you use to introduce Context across services while minimizing disruption and ensuring backward compatibility?

  2. 2

    At scale we observed that context cancellation sometimes propagates slower than expected due to blocked goroutine cleanup. How would you redesign the concurrency model or context usage to guarantee prompt cancellation across thousands of goroutines?

  3. 3

    When building a cross‑team library for request tracing and cancellation, what conventions would you enforce around context propagation, deadline setting, and value usage to avoid anti‑patterns?

Follow-up Questions

  • How would you test that cancellation is correctly propagated?
  • What could go wrong if you forget to call the cancel function?
  • Can you compare using context.WithCancel versus a simple channel for cancellation?